package com.madai.chatroom.activity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    private static final int PORT = 8080;
    private final List<Socket> mList = new ArrayList<>();

    public static void main(String[] args){
        new Server();
    }

    private Server()
    {
        try
        {
            ServerSocket server = new ServerSocket(PORT);
            //创建线程池
            ExecutorService myExecutorService = Executors.newCachedThreadPool();
            System.out.println("开启服务器...\n");
            Socket client;
            while(true)
            {
                client = server.accept();
                mList.add(client);
                myExecutorService.execute(new Service(client));
            }
        }catch(Exception e){e.printStackTrace();}
    }

    class Service implements Runnable
    {
        private final Socket socket;
        private BufferedReader in = null;
        private String msg = "";

        public Service(Socket socket) {
            this.socket = socket;
            try
            {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                msg = "用户:" +this.socket.getInetAddress() + "~加入了聊天室"
                        +"当前在线人数:" +mList.size();
                this.sendMsgToUser();
            }catch(IOException e){e.printStackTrace();}
        }

        @Override
        public void run() {
            try{
                while(true)
                {
                    if((msg = in.readLine()) != null)
                    {
                        if(msg.equals("bye"))
                        {
                            System.out.println("~~~~~~~~~~~~~");
                            mList.remove(socket);
                            in.close();
                            msg = "用户:" + socket.getInetAddress()
                                    + "退出:" +"当前在线人数:"+mList.size();
                            socket.close();
                            this.sendMsgToUser();
                            break;
                        }else{
                            msg = socket.getInetAddress() + "   说: " + msg;
                            this.sendMsgToUser();
                        }
                    }
                }
            }catch(Exception e){e.printStackTrace();}
        }

        public void sendMsgToUser()
        {
            System.out.println(msg);
            int num = mList.size();
            for(int index = 0;index < num;index++)
            {
                Socket mSocket = mList.get(index);
                PrintWriter pout;
                try {
                    pout = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(mSocket.getOutputStream(), StandardCharsets.UTF_8)),true);
                    pout.println(msg);
                }catch (IOException e) {e.printStackTrace();}
            }
        }
    }
}